package com.example.mqtt.tools;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.UUID;

@Slf4j
public class SlurryTools {

    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat milliFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat timeRangeDateFormat = new SimpleDateFormat("HH:mm");
    public static SimpleDateFormat zeroPointDateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
    public static SimpleDateFormat midnightDateFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59");

     private static final Random RANDOM = new Random();
    //雪花算法
    public final static String TOKEN = "Authorization";
    public final static String USER_ID = "USER_ID";
    public final static String REFRESH_TOKEN = "refresh-token";

    public final static Integer PLAYER = 0;
    public final static Integer COACH = 1;
    public final static Integer ADMIN_LEVEL = 2;
    public final static Integer ADMINISTRATORS_LEVEL=3;





    public final static String ADMIN = "admin";
    public final static String ADMINISTRATORS = "administrators";


    private final static String TOPIC_STR = "::---TOPIC_STR---::";


    public final static int MAN = 1;
    public final static int WOMAN = 0;

    /**
     * 生成一个不带连字符且小写的UUID
     *
     * @return UUID
     */
    public static String getUUID() {
        String s = "";
        synchronized (s) {
            s = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        }
        return s;
    }


    /**
     * 向下取整，取整个1秒
     *
     * @param timeStamp
     * @return
     */
    public static long roundDownTimestampToSecond(Long timeStamp) {
        return timeStamp / 1000 * 1000;
    }

    /**
     * 向下取整，取 0.1s 整数
     */
    public static long roundDownTimestampToTenth(Long timeStamp) {
        return (timeStamp / 100) * 100;
    }


    /**
     * 向下取整，取10秒
     */
    public static long roundDownTimestampToTenSecond(Long timeStamp) {
        return timeStamp / 10000 * 10000;
    }






    /**
     * 将分钟转化为毫秒值
     */
    public static long minuteToMillisecond(int minute) {
        return minute * 60 * 1000;
    }



    /**
     * 将时间数组 [秒, 毫秒] 转化为毫秒时间戳
     *
     * @param time 时间数组，第一个元素为秒数，第二个元素为毫秒数
     * @return 转换后的毫秒时间戳
     */
    public static Long getMillisecondTimestamp(int[] time) {
        if (time == null || time.length != 2) {
            throw new IllegalArgumentException("Time array must have exactly two elements.");
        }

        int secondTime = time[0];
        int milliTime = time[1];

        return (long) secondTime * 1000 + milliTime;
    }

    /**
     * 生成一个时间数组，其中 time[0] 表示秒数，time[1] 表示毫秒数。
     * @return int[] 时间数组
     */
    public static int[] generateTime() {
        // 获取当前时间的毫秒数
        long currentTimeMillis = System.currentTimeMillis();

        // 将毫秒数转换为秒数和毫秒数
        int seconds = (int) (currentTimeMillis / 1000);
        int milliseconds = (int) (currentTimeMillis % 1000);

        // 创建并返回时间数组
        return new int[]{seconds, milliseconds};
    }



    /**
     * 生成并返回一个包含随机 double 值的数组。
     * @return double 类型的数组
     */
    public static double[] generateRandomAngleArray() {
        Random random = new Random();
        double[] angles = new double[3];

        // 生成随机数并填充数组
        for (int i = 0; i < 3; i++) {
            // 生成范围在 [-200.0, 200.0] 的随机数
            angles[i] = -200.0 + (random.nextDouble() * 400.0);
        }

        return angles;
    }





    public static String getSplitTopic(String topic, String rowingNo) {
        return topic + TOPIC_STR + rowingNo;
    }

    public static String getRowingNo(String splitSid) {
        if (StringUtils.isBlank(splitSid)) {
            return null;
        }
        String[] split = splitSid.split(TOPIC_STR);
        return split[split.length - 1];
    }

     public static double generateRandomVelocity() {
        // 生成 1.00 到 10.00 之间的随机速度
        return 1.0 + RANDOM.nextDouble() * 9.0;
    }



}
